﻿using System;
using System.Data;

namespace ltModel.BLL
{
    /// <summary>
    /// 业务逻辑类loginlog 的摘要说明。
    /// </summary>
    public class loginlog
    {
        private readonly ltModel.DAL.loginlog dal = new ltModel.DAL.loginlog();
        public loginlog()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return dal.Exists(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ltModel.Model.loginlog model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(ltModel.Model.loginlog model)
        {
            dal.Update(model);
        }

        /// <summary>
        /// 按条件删除数据
        /// </summary>
        /// <param name="strWhere">条件</param>
        public void Delete(string strWhere)
        {
            dal.Delete(strWhere);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int id)
        {
            dal.Delete(id);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ltModel.Model.loginlog GetModel(int id)
        {
            return dal.GetModel(id);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return dal.GetList("");
        }

        public int RecordCount(string sql)
        {
            return dal.GetList(sql).Tables[0].Rows.Count;
        }

        public bool HasRecord(string sql)
        {
            return RecordCount(sql) > 0;
        }

        /// <summary>
        /// 用户是否被锁定
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public bool IsIpLocked(string ip)
        {
            return HasRecord(string.Format(" ip ='{0}' and type = '锁定' ", ip));
        }

        /// <summary>
        /// 用户登录是否太频繁
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public bool IsLogTanto(string ip)
        {
            bool isTanto = RecordCount(string.Format(" ip ='{0}' and datediff(minute,dat,getdate())<=2 and zt='失败' and type<>'锁定' ", ip)) >= 10;
            if (isTanto)
            {
                ltModel.Model.loginlog lbo = new ltModel.Model.loginlog();
                lbo.dat = DateTime.Now.ToString();
                lbo.id = 0;
                lbo.ip = ip;
                lbo.type = "锁定";
                lbo.username = ip;
                lbo.zt = "失败";
                lbo.txt = "登录太频繁！";
                dal.Add(lbo);
                return true;
            }
            return false;
        }

        public bool IsLogTanto2(string ip)
        {
            return RecordCount(string.Format(" ip ='{0}' and datediff(minute,dat,getdate())<=1 and zt='失败' and type<>'锁定' ", ip)) >= 5;
        }

        public void AddSuccessLog(string ip, string username)
        {
            AddSuccessLog(ip, username, "登录成功！");
        }

        public void AddSuccessLog(string ip, string username, string text)
        {
            ltModel.Model.loginlog lbo = new ltModel.Model.loginlog();
            lbo.dat = DateTime.Now.ToString();
            lbo.id = 0;
            lbo.ip = ip;
            lbo.type = "一般";
            lbo.username = username;
            lbo.zt = "成功";
            lbo.txt = text;
            dal.Add(lbo);
        }

        public void AddFailedLog(string ip, string username)
        {
            ltModel.Model.loginlog lbo = new ltModel.Model.loginlog();
            lbo.id = 0;
            lbo.ip = ip;
            lbo.type = "一般";
            lbo.username = username;
            lbo.zt = "失败";
            lbo.txt = "用户名或密码错误！";
            dal.Add(lbo);
        }
        #endregion  成员方法
    }
}